/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.ai2.handler;

import com.aionengine.gameserver.configs.main.CustomConfig;
import com.aionengine.gameserver.ai2.AIState;
import com.aionengine.gameserver.ai2.NpcAI2;
import com.aionengine.gameserver.ai2.event.AIEventType;
import com.aionengine.gameserver.ai2.poll.AIQuestion;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.Npc;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.npc.NpcTemplateType;
import com.aionengine.gameserver.questEngine.QuestEngine;
import com.aionengine.gameserver.questEngine.model.QuestEnv;
import com.aionengine.gameserver.services.TribeRelationService;
import com.aionengine.gameserver.utils.MathUtil;
import com.aionengine.gameserver.world.geo.GeoService;

/**
 * @author ATracer
 */
public class CreatureEventHandler {

    /**
     * @param npcAI
     * @param creature
     */
    public static void onCreatureMoved(NpcAI2 npcAI, Creature creature) {
        checkAggro(npcAI, creature);
        if (creature instanceof Player) {
            Player player = (Player) creature;
            QuestEngine.getInstance().onAtDistance(new QuestEnv(npcAI.getOwner(), player, 0, 0));
        }
    }

    /**
     * @param npcAI
     * @param creature
     */
    public static void onCreatureSee(NpcAI2 npcAI, Creature creature) {
        checkAggro(npcAI, creature);
        if (creature instanceof Player) {
            Player player = (Player) creature;
            QuestEngine.getInstance().onAtDistance(new QuestEnv(npcAI.getOwner(), player, 0, 0));
        }
    }


    /**
     * @param ai
     * @param creature
     */
    protected static void checkAggro(NpcAI2 ai, Creature creature) {
        Npc owner = ai.getOwner();

        if (ai.isInState(AIState.FIGHT))
            return;

        if (creature.getLifeStats().isAlreadyDead())
            return;

        if (!owner.canSee(creature))
            return;

        if (!owner.getActiveRegion().isMapRegionActive())
            return;

        boolean isInAggroRange = false;

        if (ai.poll(AIQuestion.CAN_SHOUT)) {
            int shoutRange = owner.getObjectTemplate().getMinimumShoutRange();
            double distance = MathUtil.getDistance(owner, creature);
            if (distance <= shoutRange) {
                ShoutEventHandler.onSee(ai, creature);
                isInAggroRange = shoutRange <= owner.getAggroRange();
            }
        }

        if (!ai.isInState(AIState.FIGHT)
                && (isInAggroRange || MathUtil.isIn3dRange(owner, creature, owner.getAggroRange()))) {
            if (checkAggroRelation(owner, creature) && GeoService.getInstance().canSee(owner, creature)) {
                if (!ai.isInState(AIState.RETURNING))
                    ai.getOwner().getMoveController().storeStep();
                if (ai.canThink())
                    ai.onCreatureEvent(AIEventType.CREATURE_AGGRO, creature);
            }
        }
    }

    private static boolean checkAggroRelation(Npc owner, Creature creature) {
        if (TribeRelationService.isAggressive(owner, creature)) {
            if (creature.getLevel() - owner.getLevel() <= CustomConfig.AGGRO_LV_DIFFERENCE || owner.getObjectTemplate().getNpcTemplateType() == NpcTemplateType.ABYSS_GUARD) {
                return true;
            }
        }
        return false;
    }
}